package com.tianji.mq.consumer;

import com.tianji.common.constant.MqConstants;
import com.tianji.medical.pojo.PatientMedication;
import com.tianji.medical.service.MedicationSendLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;

import static com.tianji.common.constant.MqConstants.CONSUMER;
import static com.tianji.common.constant.MqConstants.TOPIC;

@Component
@RocketMQMessageListener(topic = TOPIC,consumerGroup = CONSUMER)
@RequiredArgsConstructor
@Slf4j
public class MedicationRemindConsumer implements RocketMQListener<PatientMedication> {

    private final MedicationSendLogService medicationSendLogService;
    private final RocketMQTemplate rocketMQTemplate;

    //消息消费失败：处理逻辑中出现异常，如数据库连接失败、API 调用失败等。
    @Override
    public void onMessage(PatientMedication message) {
        try{
            //调用微信公众号api发送提醒信息
            sendWeChatReminder(message);
            log.info("微信提醒发送成功");
        }catch (Exception e){
            //记录失败日志和发送死信队列
            log.info("微信提醒发送失败");
            e.printStackTrace();
            medicationSendLogService.logFailedSend(message, e.getMessage());
            rocketMQTemplate.convertAndSend(MqConstants.TOPIC_DLQ,message);
        }
    }

    public void sendWeChatReminder(PatientMedication message) {
    }
}
